//
//  SY_Activity.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 16/9/7.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_Activity.h"
#import "MBProgressHUD.h"

#import "SplashScreenView.h"
#import "SYGuideViewController.h"
#import "SN_WebViewController.h"
#import "SN_VIPCenterViewController.h"
#import "SN_NewspaperColection_ViewController.h"
#import "SN_QuestionCollect_Controller.h"
#import "SN_NewspaperInfoViewController.h"
#import "SN_Question_ResolveController.h"
#import "SN_ReadRecordViewController.h"
#import "SN_PersonalInfo_ViewController.h"
#import "SN_Setting_ViewController.h"

@interface SY_Activity ()

@property(nonatomic,strong) NSMutableArray *classeids;

@end


@implementation SY_Activity 
{
    
    MainViewController *_mainVC;
    
    UIViewController *_currentVC;
    
    NSInteger _activeOrder;
    
    MBProgressHUD *hud;
    
    SY_Activity_Model *activityModel;
    
    UIViewController *moveToVC;
}


static SY_Activity *activity = nil;

+ (instancetype)shareActivity {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        activity = [[SY_Activity alloc] init];
    });
    
    return activity;
}

- (id)init
{
    self = [super init];
    if (self) {
        _activeOrder = 0;
    }
    return self;
}


-(void)action {
    
    SY_Activity_Model *model = [self.instructionArr objectAtIndex:_activeOrder];
    activityModel = model;
    
    switch (model.apptype) {
        case 0:
            _activityType = SYActivityTypeText;
            break;
        case 1:
            _activityType = SYActivityTypeBanner;
            break;
        case 2:
            _activityType = SYActivityTypeInstruction;
            break;
        case 3:
            _activityType = SYActivityTypeWeb;
            break;
            
        default:
            break;
    }
    
    if (_activityType == SYActivityTypeText) {
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        
        hud.delegate = self;
        hud.mode = MBProgressHUDModeText;
        hud.detailsLabelText = model.scontent;
        hud.detailsLabelFont = [UIFont systemFontOfSize:17];
        hud.margin = 10.f;
        hud.yOffset = 150;
        hud.removeFromSuperViewOnHide = YES;
        
        [hud showAnimated:YES whileExecutingBlock:nil];
        [hud hide:YES afterDelay:model.showtime];
        
        
    }else if (_activityType == SYActivityTypeBanner) {
        
        [model.fileid componentsSeparatedByString:@","];
        
        SYGuideViewController *guide=[[SYGuideViewController alloc]init];
        guide.webImageNameArr = [model.fileid componentsSeparatedByString:@","];
        guide.isSkip = model.isskip;
        guide.viewWasHide = ^(){
            if (model.moveto) {
                
                UIViewController * targetVC = [self getMoveTotargetWithNum:model.moveto];
                
                if (targetVC) {
                    if (model.moveto == 2102) {
                        
                        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:targetVC];
                        
                        [self->_currentVC presentViewController:nav animated:YES completion:nil];
                        
                    }else {
                        [self->_currentVC.navigationController pushViewController:targetVC animated:YES];
                    }
                    
                }else {
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"您的身份不能进行此操作" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                }
            }
            [self nextAction];
        };
        _currentVC = [Utils getCurrentVC];
        [_currentVC.navigationController presentViewController:guide animated:YES completion:nil];
        
        
    }else if (_activityType == SYActivityTypeInstruction){
        
        _currentVC = [Utils getCurrentVC];
        
        if (model.motion) {
            SYGuideViewController *guide=[[SYGuideViewController alloc]init];
            guide.guideType = [model.motion integerValue];
            guide.isSkip = model.isskip;
            guide.viewWasHide = ^(){
                
                if (model.moveto) {
                    
                    if (model.moveto == 2001) {
                        NSURL *cleanURL = [NSURL URLWithString:model.url];
                        [[UIApplication sharedApplication] openURL:cleanURL];
                        
                        return;
                    }
                    UIViewController * targetVC = [self getMoveTotargetWithNum:model.moveto];
                    
                    if (targetVC) {
                        if (model.moveto == 2102) {
                            
                            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:targetVC];
                            
                            [self->_currentVC presentViewController:nav animated:YES completion:nil];
                            
                        }else {
                            [self->_currentVC.navigationController pushViewController:targetVC animated:YES];
                        }
                        
                    }else {
                        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"当前版本不支持" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                        [alertView show];
                    }
                }
                [self nextAction];
            };
            
            [_currentVC.navigationController presentViewController:guide animated:YES completion:nil];
            
        }else {
            
            _currentVC = [Utils getCurrentVC];
            
            if (model.moveto) {
                
                if (model.moveto == 2001) {
                    NSURL *cleanURL = [NSURL URLWithString:model.url];
                    [[UIApplication sharedApplication] openURL:cleanURL];
                    
                    return;
                }
                UIViewController * targetVC = [self getMoveTotargetWithNum:model.moveto];
                
                if (targetVC) {
                    if (model.moveto == 2102) {
                        
                        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:targetVC];
                        
                        [self->_currentVC presentViewController:nav animated:YES completion:nil];
                        
                    }else {
                        [self->_currentVC.navigationController pushViewController:targetVC animated:YES];
                    }
                }else {
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"当前版本不支持" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alertView show];
                }
            }
            [self nextAction];
        }
        
        
        
    }
    
}

-(void)hudWasHidden:(MBProgressHUD *)hud {
    
    NSLog(@"隐藏了");
    [self nextAction];
    
}

#pragma mark - 执行完之后判断后面还有别的任务没有

- (void)nextAction{
    
    _activeOrder += 1;
    if (_activeOrder < [self.instructionArr count]) {
        [self action];
    }else {
        _activeOrder = 0;
    }
}


- (UIViewController *)getMoveTotargetWithNum:(NSInteger )num {
    
    moveToVC = nil;
    
    if (num == 2001) {
        
        NSURL *cleanURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", activityModel.url]];
        [[UIApplication sharedApplication] openURL:cleanURL];
        
        
    }else if (num == 2002) {
        SN_WebViewController *targetVC = [[SN_WebViewController alloc]init];
        targetVC.url = activityModel.url;
        
        moveToVC = targetVC;
    }else if (num == 3001) {
        
    }else if (num == 3002) {
        
        SN_NewspaperInfoViewController *targetVC = [[SN_NewspaperInfoViewController alloc]init];
        targetVC.newsid = activityModel.infoid;
        
        moveToVC = targetVC;
        
    }else if (num == 3003) {
        
        SN_ReadRecordViewController *targetVC = [[SN_ReadRecordViewController alloc]init];
        
        moveToVC = targetVC;
        
    }else if (num == 3101) {
        
    }else if (num == 3102) {
        SN_Question_ResolveController *targetVC = [[SN_Question_ResolveController alloc]init];
        targetVC.newsId = activityModel.infoid;
        
        moveToVC = targetVC;
        
    }else if (num == 3201) {
        
    }else if (num == 3202) {
        
        SN_PersonalInfo_ViewController *targetVC = [[SN_PersonalInfo_ViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 3203) {
        
        SN_NewspaperColection_ViewController *targetVC = [[SN_NewspaperColection_ViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 3204) {
        
        SN_QuestionCollect_Controller *targetVC = [[SN_QuestionCollect_Controller alloc]init];
        
        moveToVC = targetVC;
        
    }else if (num == 3205) {
        
        SN_VIPCenterViewController *targetVC = [[SN_VIPCenterViewController alloc]init];
        
        moveToVC = targetVC;
    }else if (num == 3206) {
        
        SN_Setting_ViewController *targetVC = [[SN_Setting_ViewController alloc] init];
        moveToVC = targetVC;
    }
    

    return moveToVC;
}

@end
